package com.example.recoderandcamera;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.io.File;

public class CameraActivity extends Activity {
    private Button btn_sysPho, btn_cusPho;
    private ImageView phoRes;
    private static final String FILE_PATH = "/sdcard/syscamera.jpg";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        init();
        bind();
        Intent intent=getIntent();
        String path=intent.getStringExtra("path");
        System.out.println(path);
        if (path!=null&&!path.equals("")){
            phoRes.setImageURI(Uri.fromFile(new File(path)));
//            phoRes.setPivotX(phoRes.getWidth()/2);
//            phoRes.setPivotY(phoRes.getHeight()/2);
//            phoRes.setRotation(90);
        }
    }

    private void bind() {
        btn_sysPho.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //去寻找是否已经有了相机的权限
                if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
                    //Toast.makeText(MainActivity.this,"您申请了动态权限",Toast.LENGTH_SHORT).show();
                    //如果有了相机的权限有调用相机
                    startCamera();
                } else {
                    //否则去请求相机权限
                    ActivityCompat.requestPermissions(CameraActivity.this, new String[]{Manifest.permission.CAMERA}, 100);
                }
            }
        });
        btn_cusPho.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getApplicationContext(),CusCameraActivity.class);
                startActivity(intent);
            }
        });
    }

    //    onRequestPermissionsResult
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            //如果requestCode为100 就走这里
            case 100:
                //permissions[0].equals(Manifest.permission.CAMERA)
                //grantResults[0] == PackageManager.PERMISSION_GRANTED
                //上面的俩个判断可有可无
                if (permissions[0].equals(Manifest.permission.CAMERA)) {
                    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        //如果用户同意了再去打开相机
                        Toast.makeText(CameraActivity.this, "非常感谢您的同意,再会", Toast.LENGTH_SHORT).show();
                        startCamera();
                    } else {
                        //因为第一次的对话框是系统提供的 从这以后系统不会自动弹出对话框 我们需要自己弹出一个对话框
                        //进行询问的工作
                        startAlertDiaLog();
                    }
                }
                break;
        }
    }

    public void startCamera() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 20);
    }

    public void startAlertDiaLog() {
        AlertDialog.Builder alert = new AlertDialog.Builder(CameraActivity.this);
        alert.setTitle("说明");
        alert.setMessage("需要相机权限 去拍照");
        alert.setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //这里的立即开启 是打开手机的设置页面(打开相机权限)
                startSetting();
            }
        });
        alert.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //如果用户还不打开 只能等用户下次点击时再次询问
                Toast.makeText(CameraActivity.this, "当您点击我们会再次询问", Toast.LENGTH_SHORT).show();
            }
        });
        alert.create();
        alert.show();
    }

    public void startSetting() {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.fromParts("package", getPackageName(), null);
        intent.setData(uri);
        startActivityForResult(intent, 10);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10 && resultCode == RESULT_OK) {
            if (ContextCompat.checkSelfPermission(CameraActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(CameraActivity.this, "非常感谢您的同意", Toast.LENGTH_SHORT).show();
            }
        }
        if (requestCode==20){
            System.out.println(">>>>>success>>>>>");
            phoRes.setImageBitmap((Bitmap) data.getParcelableExtra("data"));
        }
    }

    private void init() {
        btn_cusPho = findViewById(R.id.btn_cusPho);
        btn_sysPho = findViewById(R.id.btn_sysPho);
        phoRes = findViewById(R.id.phoRes);
    }
}
